package com.kfm.base.arrays;

public class Exer08 {

    public static void main(String[] args) {

        int[][] nums = {
                {0, 0, 1, 0, 1, 0, 0},
                {0, 0, 1, 0, 0, 0, 1},
                {0, 0, 1, 0, 1, 0, 1},
                {1, 0, 1, 0, 1, 0, 1},
                {1, 1, 1, 1, 1, 1, 1},
        };

        int water = 0;
        outer: for (int i = nums.length - 2; i >= 0; i --) {
            int left = 0;
            int right = nums[i].length - 1;
            // 从左侧找到边界 1
            while(left < right){
                if (nums[i][left] == 1 && nums[i][left + 1] == 0){
                    break;
                }
                left ++;
            }

            // 从右边找边界 1
            while(left < right){
                if (nums[i][right] == 1 && nums[i][right - 1] == 0){
                    break;
                }
                right --;
            }

            // 一种情况 left = right, 说明只有一个 1
            if (left != right){
                water += right - left - 1;

                for (int j = left + 1; j < right; j++) {
                    if (nums[i][j] == 1){
                        water --;
                    }
                }
            }

        }
        System.out.println("能存" + water + "水");
    }
}
